package com.mywang.weitravel.modules.find;

import com.mywang.weitravel.base.BasePresenter;
import com.mywang.weitravel.model.FindListBean;
import com.mywang.weitravel.net.RetrofitHelper;
import com.mywang.weitravel.net.RxSchedulers;
import com.mywang.weitravel.net.VObserver;

import java.util.HashMap;

import io.reactivex.disposables.Disposable;

/**
 * Created by wangmingyun on 2019/12/5.
 */
public class FindPresenter extends BasePresenter<FindContract.View> implements FindContract.Presenter {

    public FindPresenter(FindContract.View view) {
        super(view);
    }

    @Override
    public void getFindList(int page) {
        HashMap<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));

        RetrofitHelper.getInstance().getServer()
                .getFindList(params)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<FindListBean>() {
                    @Override
                    protected void onSuccess(FindListBean findListBean) {
                        mView.getFindListSuccess(findListBean);
                    }

                    @Override
                    protected void onFailure(int code, String message, boolean isShowMsg) {
                        super.onFailure(code, message, isShowMsg);
                        mView.getFindListError();
                    }
                });

    }

    @Override
    public void collect(String findId) {
        RetrofitHelper.getInstance().getServer()
                .collect(findId)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new VObserver<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        mView.showLoading();
                    }

                    @Override
                    protected void onSuccess(Object object) {
                        mView.collectSuccessful(findId);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        mView.hideLoading();
                    }
                });
    }
}
